﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using V5_Common;
using System.Web.Compilation;
using System.IO;
using System.Text.RegularExpressions;

namespace V5_Data {
    public class V5HtmlHelper {
        private static readonly V5HtmlHelper m_Instance = new V5HtmlHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5HtmlHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 在模板目录下获取包含文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string Include(string filePath) {
            try {
                return V5.Html.Include("~/Template/" + V5.Config.TemplateDir + "/" + filePath);
            }
            catch (Exception ex) {
                return ex.Message;
            }
        }
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string Partial(string filePath) {
            //string templatePath = "/Template/" + V5.Config.TemplateDir + "/" + filePath + ".cshtml";
            //var path = System.IO.Path.Combine(HttpContext.Current.Request.ApplicationPath, templatePath);
            //var type = BuildManager.GetCompiledType(path);
            //var script = Activator.CreateInstance(type) as System.Web.WebPages.WebPage;
            //script.VirtualPath = "~" + templatePath;
            ////
            //var stringWriter = new StringWriter();
            //script.ExecutePageHierarchy(new WebPageContext(
            //        new HttpContextWrapper(HttpContext.Current), script, null
            //        ), stringWriter);
            //return new HtmlString(stringWriter.ToString());
            return string.Empty;
        }

        /// <summary>
        /// 请求参数截取    Param1!111!!Param2!222
        /// RequestVal("Param1!111!!Param2!222","Param1")=111
        /// RequestVal("Param1!111!!Param2!222","Param2")=222
        /// </summary>
        /// <param name="Tag">请求所有的参数</param>
        /// <param name="TagName">参数名称</param>
        /// <returns></returns>
        public string RequestVal(string Tag, string TagName) {
            string[] strArray = Tag.Split(new string[] { "!!" }, StringSplitOptions.None);
            for (int i = 0; i < strArray.Length; i++) {
                Regex regex = new Regex(@"(?<Keyword>\w+)\s*!\s*(?<Value>.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                for (Match match = regex.Match(strArray[i]); match.Success; match = match.NextMatch()) {
                    if (match.Groups["Keyword"].ToString().ToLower().IndexOf(TagName.ToLower()) != -1) {
                        return match.Groups["Value"].ToString().ToLower();
                    }
                }
            }
            return "";
        }


        #region 生成静态帮助
        /// <summary>
        /// 进度状态
        /// </summary>
        /// <param name="c"></param>
        /// <param name="t"></param>
        public void ProcessBar(int c, int t) {
            HttpContext.Current.Response.Write("<script>setPgb(" + ((c + 1) * 100) / t + ");</script>");
            HttpContext.Current.Response.Flush();
        }
        /// <summary>
        /// 初始化进度
        /// </summary>
        /// <param name="tit"></param>
        /// <param name="IsInit"></param>
        public void SetProcessBar(string tit, bool IsInit) {
            if (IsInit) {
                string strFileName = HttpContext.Current.Request.PhysicalApplicationPath + "\\Includes\\progressbar.html";
                System.IO.StreamReader sr = new System.IO.StreamReader(strFileName, System.Text.Encoding.Default);
                string strHtml = sr.ReadToEnd();
                HttpContext.Current.Response.Write(strHtml);
                sr.Close();
                HttpContext.Current.Response.Flush();
            }
            HttpContext.Current.Response.Write("<script>initPgb('" + tit + "');</script>");
            HttpContext.Current.Response.Flush();
        }
        public void SetProcessMessage(string Msg) {
            HttpContext.Current.Response.Write("<script>setMessageBox('" + Msg + "');</script>");
            HttpContext.Current.Response.Flush();
        }
        #endregion

        /// <summary>
        /// 输出静态帮助
        /// </summary>
        /// <param name="strVal"></param>
        /// <returns></returns>
        public HtmlString Out(object strVal) {
            return new HtmlString(strVal.ToString());
        }
    }
}